package jmine.tec.web.wicket.component.table.dataprovider;

import java.util.List;

import jmine.tec.web.wicket.BeanReference;
import jmine.tec.web.wicket.component.command.button.SearchCommandHandler;
import jmine.tec.web.wicket.component.paging.DAOQueryDataProvider;
import jmine.tec.web.wicket.model.DefaultDetachableModel;

import org.apache.wicket.model.IModel;

import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.persist.hibernate.page.PagingService;

/**
 * DataProvider que executa um {@link SearchCommandHandler} para buscar dos dados.
 * 
 * @author Rafael Volpato
 * @date Aug 8, 2011
 * @param <T> Tipo da entidade
 */
public class SearchHandlerDataProvider<T> extends DAOQueryDataProvider<T> {

    private final SearchCommandHandler<T> handler;

    /**
     * Construtor.
     * 
     * @param pageSize pageSize
     * @param daoFactoryReference {@link BaseDAOFactory}
     * @param pagingServiceReference {@link PagingService}
     * @param handler {@link SearchCommandHandler}
     */
    public SearchHandlerDataProvider(int pageSize, BeanReference<BaseDAOFactory> daoFactoryReference,
            BeanReference<PagingService> pagingServiceReference, SearchCommandHandler<T> handler) {
        super(pageSize, daoFactoryReference, pagingServiceReference);
        this.handler = handler;
    }

    /**
     * {@inheritDoc}
     */
    public IModel<T> model(T object) {
        return new DefaultDetachableModel<T>(object);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<T> makeQuery(BaseDAOFactory daoFactory) {
        return this.handler.search(daoFactory);
    }
}
